package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.adapters.CourseAdapter;
import com.example.myapplication.models.Course;
import com.example.myapplication.network.RetrofitClient;

import java.util.List;

public class CategoryListActivity extends AppCompatActivity {

    private RecyclerView rvCourses;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_list);

        int categoryId = getIntent().getIntExtra("category_id", 0);
        String categoryName = getIntent().getStringExtra("category_name");

        TextView tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText(categoryName + "课程");

        rvCourses = findViewById(R.id.rv_courses);

        setupCourses(categoryId);
    }

    private void setupCourses(int categoryId) {
        RetrofitClient.getInstance().getCoursesByCategory(categoryId, new RetrofitClient.ApiCallback<List<Course>>() {
            @Override
            public void onSuccess(List<Course> courses) {
                GridLayoutManager layoutManager = new GridLayoutManager(CategoryListActivity.this, 2);
                rvCourses.setLayoutManager(layoutManager);

                CourseAdapter adapter = new CourseAdapter(courses, course -> {
                    Intent intent = new Intent(CategoryListActivity.this, CourseDetailActivity.class);
                    intent.putExtra("course_id", course.getId());
                    startActivity(intent);
                });
                rvCourses.setAdapter(adapter);
            }

            @Override
            public void onError(String message) {
                Toast.makeText(CategoryListActivity.this, "加载课程失败: " + message, Toast.LENGTH_SHORT).show();
            }
        });
    }
}